package nio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ReadAndWriteLine {

	public static void main(String[] args) {

		File filein = new File("file/textin.txt");
		File fileout = new File("file/textout-nio.txt");
		try {
			RandomAccessFile fin = new RandomAccessFile(filein, "r");
			FileOutputStream fout = new FileOutputStream(fileout);
			FileChannel inChannel = fin.getChannel();
			FileChannel outChannel = fout.getChannel();
			
			ByteBuffer buf = ByteBuffer.allocate(48);
			
			int bytesRead = inChannel.read(buf);
			while(bytesRead != -1){
				buf.flip();

				outChannel.write(buf);
				while(buf.hasRemaining()){
					System.out.print((char) buf.get());
				}
				buf.clear();
				bytesRead = inChannel.read(buf);
				
			}
			fin.close();
			fout.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
